package com.zhuozhou.jdyc.main.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityMerchantBinding;
import com.zhuozhou.jdyc.databinding.IncludeGoodsScreenBinding;
import com.zhuozhou.jdyc.main.goods.activity.GoodsDetailActivity;
import com.zhuozhou.jdyc.main.main.adapter.IndexShopAdapter;
import com.zhuozhou.jdyc.main.main.bean.IndexDetailsBean;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.dialog.CallDialog;
import com.zhuozhou.jdyc.wight.dialog.LoginDialog;
import com.zhuozhou.jdyc.wight.dialog.MapDialog;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;

/**
 * Create by Once_ChuXy on 2021/8/17
 * Describe:商家详情
 */
public class MerchantActivity extends BaseActivity<MainViewModel, ActivityMerchantBinding> implements IndexShopAdapter.OnShopItemClickListener {


    private IndexShopAdapter indexShopAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant;
    }

    @Override
    public String setTitleBar() {
        return "商城店铺详情";
    }

    @Override
    public void initView() {
        mViewModel.shopIndexDetailsBeanMutableLiveData.observe(this, v -> {
            binding.setDetail(v.getList());
            Glide.with(this).load(v.getList().getImages()).error(R.mipmap.icon_head_default).into(binding.imageMerchant);
            indexShopAdapter = new IndexShopAdapter(this);
            indexShopAdapter.setList(v.getList().getList());
            indexShopAdapter.setOnShopItemClickListener(this);
            binding.mRecyclerViewShop.setAdapter(indexShopAdapter);
            binding.mRecyclerViewShop.setLayoutManager(new FullyGridLayoutManager(this, 2) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            binding.imgTel.setOnClickListener(i -> {
                new CallDialog(this, v.getList().getPhone()).call();
            });
            binding.tvLocation.setOnClickListener(k -> {
                new MapDialog(this)
                        .setLocation(new LatLng(Double.parseDouble(v.getList().getX()), Double.parseDouble(v.getList().getY())))
                        .builder()
                        .show();
            });
            binding.tvAttention.setOnClickListener(t -> {
                if (StringUtil.isNotEmpty(UserAccountHelper.getUserID()))
                    mViewModel.getBasicsFollow(baseBundle.getString("id"), UserAccountHelper.getUserID());
                else
                    new LoginDialog(this).builder().show();
            });
            binding.tvAttention.setBackground(v.getList().getType() == 0 ? getResources().getDrawable(R.drawable.rounded_red_10) : getResources().getDrawable(R.drawable.rounded_gray_10));
        });
        mViewModel.BasicsFollowMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            IndexDetailsBean.ListBeanX beanX;
            beanX = binding.getDetail();
            beanX.setType(v.getType());
            beanX.setVermicelli(binding.getDetail().getVermicelli() + (v.getType() == 0 ? -1 : 1));
            binding.setDetail(beanX);
            binding.tvAttention.setBackground(v.getType() == 0 ? getResources().getDrawable(R.drawable.rounded_red_10) : getResources().getDrawable(R.drawable.rounded_gray_10));
        });
        IncludeGoodsScreenBinding screenBinding = binding.includeGoodsScreen;
        screenBinding.llTopPrice.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.VISIBLE);
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
            screenBinding.tvTopSynthesis.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
            screenBinding.tvTopPrice.setTextColor(this.getResources().getColor(R.color.themeColor));
            screenBinding.tvTopSynthesis.setTextColor(this.getResources().getColor(R.color.text_desc_color));
        });
        screenBinding.llTopSynthesis.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.llSynthesis.setVisibility(View.VISIBLE);
            screenBinding.tvTopSynthesis.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
            screenBinding.tvTopPrice.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
            screenBinding.tvTopSynthesis.setTextColor(this.getResources().getColor(R.color.themeColor));
            screenBinding.tvTopPrice.setTextColor(this.getResources().getColor(R.color.text_desc_color));
        });
        screenBinding.tvSynthesis.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 0);
        });
        screenBinding.tvSales.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSales.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 1);
        });
        screenBinding.tvOnTheNew.setOnClickListener(v -> {
            screenBinding.llSynthesis.setVisibility(View.GONE);
            screenBinding.tvTopSynthesis.setText(screenBinding.tvOnTheNew.getText().toString());
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 2);
        });
        screenBinding.tvPrice.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPrice.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 0);
        });
        screenBinding.tvPriceDescending.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPriceDescending.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 3);
        });
        screenBinding.tvPriceAscending.setOnClickListener(v -> {
            screenBinding.llPrice.setVisibility(View.GONE);
            screenBinding.tvTopPrice.setText(screenBinding.tvPriceAscending.getText().toString());
            screenBinding.tvTopSynthesis.setText(screenBinding.tvSynthesis.getText().toString());
            mViewModel.getShopIndexDetails(baseBundle.getString("id"), UserAccountHelper.getUserID() + "", 4);
        });

    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getShopIndexDetails(bundle.getString("id"), UserAccountHelper.getUserID() + "", 1);
    }

    @Override
    public void onShopItemClick(View view, int position) {
        Intent intent = new Intent(this, GoodsDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", indexShopAdapter.getList().get(position).getCommodity_id() + "");
        bundle.putInt("type", 1);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
